package com.purchase.common.api.ext;


import cn.com.flaginfo.sdk.cmc.api.AbstractApi;
import cn.com.flaginfo.sdk.cmc.api.request.ApiConfig;
import cn.com.flaginfo.sdk.cmc.api.result.ComResult;
import cn.com.flaginfo.sdk.cmc.common.Constants;
import cn.com.flaginfo.sdk.cmc.common.HttpMsg;
import cn.com.flaginfo.sdk.cmc.common.ResponseCode;
import cn.com.flaginfo.sdk.cmc.util.JSONUtil;
import cn.com.flaginfo.sdk.cmc.util.TypeRef;
import com.purchase.common.api.ext.model.ExtApiRequest;
import com.purchase.common.api.ext.model.ExtApiResult;

/**
 * 拓展接口API的方式
 */
public class ExtSDKDemoApi extends AbstractApi<ExtApiRequest, ExtApiResult> {

	public ExtSDKDemoApi(ApiConfig apiConfig) {
		super(apiConfig);
	}

	@Override
	protected ResponseCode checkParameter(ExtApiRequest extApiRequest) {
		if (getRequestUrl() == null
				|| getRequestUrl().trim().length() == 0
				|| extApiRequest.getUserNumber() == null
				|| extApiRequest.getUserNumber().trim().length() == 0
				|| extApiRequest.getTemplateId() == null
				|| extApiRequest.getTemplateId().trim().length() == 0
				|| extApiRequest.getMessageContent() == null
				|| extApiRequest.getMessageContent().trim().length() == 0) {
			return ResponseCode.MISS_PARAM_ERROR;
		}
		return ResponseCode.OK;
	}

	@Override
	public String getRequestUrl() {
		if (requestUrl == null || "".equals(requestUrl.trim())) {
			requestUrl = Constants.BASE_URL_SEND + "/api/sms/send";
		}
		return requestUrl;
	}


	@Override
	protected ComResult<ExtApiResult> responseBodyConvert(HttpMsg httpMsg) {
		ComResult<ExtApiResult> result = JSONUtil.parseToObject(httpMsg.getBody(), new TypeRef<ComResult<ExtApiResult>>() {
		});
		return result;
	}

}
